package it.enricod.jcontextfree.engine.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author edonelli
 */
public class IOUtils {

    /**
     * @param filePath      name of file to open. The file can reside
     *                      anywhere in the classpath
     */
    public static String readFileAsString(String filePath) {
        try {
            StringBuffer fileData = new StringBuffer(1000);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    IOUtils.class.getClassLoader().getResourceAsStream(filePath)));
            char[] buf = new char[1024];
            int numRead = 0;
            while ((numRead = reader.read(buf)) != -1) {
                String readData = String.valueOf(buf, 0, numRead);
                fileData.append(readData);
                buf = new char[1024];
            }
            reader.close();
            return fileData.toString();
        } catch (IOException ex) {
            throw new ExceptionAdapter(ex);
        }
    }
}
